package dao;

import entity.Note;
import entity.Remark;
import entity.Users;
import utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RemarkDao {

    public List<Remark> findAll() throws SQLException {
        //1.创建一个用来放入数据的集合
        List<Remark> remarks = new ArrayList<>();
        Connection connection = ConnectionUtils.getConnection();
        //2.书写你要发送给仓库的命令
        String sql = "select remarkid, username, noteid,remarktext,remarkdate,remarkzan from remark";
        //3.通过连接构建一个载体，这个载体就是加载命令的
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Remark remark = new Remark();
            remark.setRemarkid( resultSet.getInt(1));
            remark.setUsername(resultSet.getString(2));
            remark.setNoteid(resultSet.getInt(3));
            remark.setRemarktext(resultSet.getString(4));
            remark.setRemarkdate(resultSet.getString(5));
            remark.setRemarkzan(resultSet.getInt(6));
            remarks.add(remark);
        }
        return remarks;

    }

    public boolean insertRemark(Remark remark) throws SQLException {
        Connection connection = ConnectionUtils.getConnection();
        //2.书写你要发送给仓库的命令
        String sql = "insert into remark(username,noteid,remarktext,remarkdate,remarkzan) VALUES(?,?,?,?,?)";

//        3.通过连接构建一个载体，这个载体就是加载命令的
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, remark.getUsername());
        preparedStatement.setInt(2,remark.getNoteid());
        preparedStatement.setString(3,remark.getRemarktext());
        preparedStatement.setString(4,remark.getRemarkdate());
        preparedStatement.setInt(5,remark.getRemarkzan());
//        System.out.println(remark.getUsername()+remark.getNoteid()+remark.getRemarktext()+remark.getRemarkdate()+remark.getRemarkzan());
        if(!preparedStatement.execute())
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    public List<Remark> findNote_Remark(int noteid) throws SQLException {
        //1.创建一个用来放入数据的集合
        List<Remark> remarks = new ArrayList<>();
        Connection connection = ConnectionUtils.getConnection();
        //2.书写你要发送给仓库的命令
        String sql = "select remarkid, username, noteid,remarktext,remarkdate,remarkzan from remark where noteid=?";
        //3.通过连接构建一个载体，这个载体就是加载命令的
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,noteid);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Remark remark = new Remark();
            remark.setRemarkid( resultSet.getInt(1));
            remark.setUsername(resultSet.getString(2));
            remark.setNoteid(resultSet.getInt(3));
            remark.setRemarktext(resultSet.getString(4));
            remark.setRemarkdate(resultSet.getString(5));
            remark.setRemarkzan(resultSet.getInt(6));
            remarks.add(remark);
        }
        return remarks;
    }

}
